package com.easylinkin.linkappapi.jzsmokewarning.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.common.model.SortModel;
import com.easylinkin.linkappapi.common.utils.StringUtil;
import com.easylinkin.linkappapi.common.utils.excel.ExcelConstant;
import com.easylinkin.linkappapi.common.utils.excel.ExcelTools;
import com.easylinkin.linkappapi.common.utils.io.OutputStreamUtil;
import com.easylinkin.linkappapi.jzsmokewarning.entity.SmokeWarning;
import com.easylinkin.linkappapi.jzsmokewarning.entity.SmokeWarningVo;
import com.easylinkin.linkappapi.jzsmokewarning.mapper.SmokeWarningMapper;
import com.easylinkin.linkappapi.jzsmokewarning.service.SmokeWarningService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 荆州油烟预警 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2021-12-20
 */
@Service
@Slf4j
public class SmokeWarningServiceImpl extends ServiceImpl<SmokeWarningMapper, SmokeWarning> implements SmokeWarningService {


    @Override
    public IPage<SmokeWarning> getSmokeCollectionPage(Page page, List<SortModel> sorts, SmokeWarning smokeWarning) {
        if (sorts != null) {
            for (SortModel sortModel : sorts) {
                if (sortModel.getField() != null) {
                    sortModel.setField(StringUtil.camel2Underline(sortModel.getField()));
                }
            }
        }
        return baseMapper.getSmokeCollectionPage(page, sorts, smokeWarning);
    }


    @Override
    public void exportData(SmokeWarning smokeWarning, HttpServletRequest request, HttpServletResponse response) {
        IPage<SmokeWarning> smokeWarningIPage = getSmokeCollectionPage(new Page(1, -1), null, smokeWarning);
        List<SmokeWarningVo> smokeWarningVos = new ArrayList<>();
        for (SmokeWarning warning : smokeWarningIPage.getRecords()) {
            smokeWarningVos.add(new SmokeWarningVo(warning));
        }
        String keyValue = "设备编号:deviceCode,设备名称:deviceName,区域位置:areaPath,时间范围:range,数据指标:attribute,周期:cycle,数据量:dataCount"
                + ",平均值:averageValue,创建时间:createTimeStr";
        String title = "油烟采集导出数据";
        String fileName = title + ".xls";
        try {
            OutputStream outputStream = OutputStreamUtil
                    .getOutputStream(request, response, fileName);
            ExcelTools.exportExcel(outputStream, keyValue, smokeWarningVos, ExcelConstant.XLS, title);
            response.flushBuffer();
            outputStream.close();
        } catch (IOException e) {
            log.error("excel导出失败", e);
            throw new RuntimeException("excel导出失败！IOException异常" + e.getMessage());
        } catch (Exception e) {
            log.error("excel导出失败", e);
            throw new RuntimeException("excel导出失败！" + e.getMessage());
        }
    }
}
